<!--
 * @Author: your name
 * @Date: 2021-11-10 15:29:01
 * @LastEditTime: 2021-12-04 21:44:51
 * @LastEditors: Please set LastEditors
 * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 * @FilePath: \li-yuwei\admin\src\views\home\articleAdmin\index.vue
-->
<template>
  <div class="allAdmin">
    <el-dialog
      title="回复评论"
      :visible.sync="dialogVisible"
      width="30%"
      :before-close="handleClose"
    >
      <a-textarea
        :rows="4"
        style="resize: none"
        v-model="replyValue"
        placeholder="支持Markdown"
      />
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="replyComment()">确 定</el-button>
      </span>
    </el-dialog>
    <div class="search-box">
      <el-card class="box-card">
        <div class="input-box">
          <div class="title-input-box">
            <div>称呼:</div>
            <el-input placeholder="请输入称呼" v-model="searchVal"> </el-input>
          </div>
          <div class="title-input-box">
            <div>Email:</div>
            <el-input placeholder="请输入联系方式" v-model="pathUrl"></el-input>
          </div>
          <div class="title-input-box">
            <div>状态:</div>
            <el-select
              v-model="state"
              placeholder="请选择"
              style="width: 190px; margin-left: 10px"
            >
              <el-option
                v-for="(item, index) in options"
                :key="index"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </div>
        </div>
        <div class="search-btn-box">
          <el-button type="primary" @click="getCommentList(1, pageSize)"
            >搜索</el-button
          >
          <el-button @click="resetVal">重置</el-button>
        </div>
      </el-card>
    </div>
    <div class="table-box">
      <el-card>
        <div class="new-create">
          <div class="batch-handler-box" v-if="multipleSelection.length">
            <el-button plain @click="batchHandlerComment('batchPassComments')"
              >通过</el-button
            >
            <el-button plain @click="batchHandlerComment('batchRefuseComments')"
              >拒绝</el-button
            >
            <el-popconfirm
              title="确定删除吗？"
              @confirm="batchDelCommentList()"
            >
              <button class="del-btn" slot="reference" type="text">删除</button>
            </el-popconfirm>
          </div>
          <div v-else></div>
          <div>
            <i class="iconfont icon-shuaxin" style="font-size: 20px"></i>
          </div>
        </div>
      </el-card>
      <div class="table-info-box">
        <el-table
          ref="multipleTable"
          :data="tableData"
          tooltip-effect="dark"
          style="width: 100%; overflow: none"
          @selection-change="handleSelectionChange"
        >
          <el-table-column type="selection" width="55" fixed> </el-table-column>
          <el-table-column label="称呼" width="100">
            <template slot-scope="scope" fixed>{{ scope.row.author }}</template>
          </el-table-column>
          <el-table-column label="状态" fixed width="200px">
            <template slot-scope="scope">
              <div v-if="scope.row.state * 1 === 0">
                <i class="iconfont icon-yuandian" style="color: #faad14"></i>
                未通过
              </div>
              <div v-else>
                <i class="iconfont icon-yuandian" style="color: #52c41a"></i>
                通过
              </div>
            </template>
          </el-table-column>
          <el-table-column label="联系方式" width="140">
            <template slot-scope="scope">{{ scope.row.email }}</template>
          </el-table-column>
          <el-table-column label="原始内容" width="140">
            <template slot-scope="scope">
              <el-popover trigger="hover" placement="top">
                <p>{{ scope.row.content }}</p>
                <div slot="reference" class="name-wrapper">
                  <el-tag size="medium">查看内容</el-tag>
                </div>
              </el-popover>
            </template>
          </el-table-column>
          <el-table-column label="HTML内容" width="140">
            <template slot-scope="scope">
              <el-popover trigger="hover" placement="top">
                <p>{{ scope.row.content }}</p>
                <div slot="reference" class="name-wrapper">
                  <el-tag size="medium">查看内容</el-tag>
                </div>
              </el-popover>
            </template>
          </el-table-column>
          <el-table-column label="管理文章" width="140">
            <template slot-scope="scope">{{
              scope.row.admin_article
            }}</template>
          </el-table-column>
          <el-table-column label="创建时间" width="200">
            <template slot-scope="scope">{{ scope.row.comment_time }}</template>
          </el-table-column>
          <el-table-column label="父级评论" width="100">
            <template slot-scope="scope">
              {{ scope.row.parent_content }}
            </template>
          </el-table-column>
          <el-table-column fixed="right" label="操作" width="240">
            <template slot-scope="scope">
              <el-button type="text" @click="passComment(scope.row.id)"
                >通过</el-button
              >
              <el-button type="text" @click="refuseComment(scope.row.id)"
                >拒绝</el-button
              >
              <el-button type="text" @click="replyOpen(scope.row)"
                >回复</el-button
              >
              <el-popconfirm
                title="确定删除吗？"
                @confirm="delCommentList(scope.row.id)"
              >
                <el-button slot="reference" type="text" style="margin-left: 5px"
                  >删除</el-button
                >
              </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <el-card class="pageination-box">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-sizes="[8, 12, 24, 36]"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        >
        </el-pagination>
      </el-card>
    </div>
  </div>
</template>

<script>
import api from "../../../api";
export default {
  data() {
    return {
      tableData: [],
      multipleSelection: [],
      currentPage: 1,
      total: null,
      pageSize: 8,
      searchVal: "", //搜索
      state: "null", //状态
      pathUrl: "",
      classifyOpt: [],
      dialogVisible: false,
      replyValue: "",
      info: {},
      options: [
        {
          value: 1,
          label: "已发布",
        },
        {
          value: 0,
          label: "草稿",
        },
      ],
    };
  },
  methods: {
    toggleSelection(rows) {
      if (rows) {
        rows.forEach((row) => {
          this.$refs.multipleTable.toggleRowSelection(row);
        });
      } else {
        this.$refs.multipleTable.clearSelection();
      }
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    //获取文章列表
    async getCommentList(pageIndex, pageSize) {
      //获取数据接口
      const res = await api.getCommentList({
        pageSize,
        pageIndex,
        author: this.searchVal,
        email: this.pathUrl,
        state: this.state,
      });
      if (res.success) {
        this.tableData = res.data.res;
        this.total = res.data.total;
      }
    },
    //改每页条数时
    handleSizeChange(val) {
      this.pageSize = val;
      this.getCommentList(this.currentPage, this.pageSize);
    },
    //改变页码时
    handleCurrentChange(val) {
      this.currentPage = val;
      this.getCommentList(val, this.pageSize);
    },
    //清空各个框 并重新获取数据
    resetVal() {
      this.state = "null";
      this.searchVal = "";
      this.pathUrl = "";
      this.getCommentList(1, this.pageSize);
    },
    //删除列表（单个）
    async delCommentList(id) {
      //调用删除接口
      const res = await api.delCommentList(id);
      //判断是否成功
      if (res.success) {
        //重新渲染
        this.getCommentList(this.currentPage, this.pageSize);
        //成功提示
        this.$message.success("删除成功");
      }
    },
    //删除列表（批量）
    async batchDelCommentList() {
      const res = await api.batchDelComments({
        batchList: this.multipleSelection,
      });
      //判断是否成功
      if (res.success) {
        //重新渲染
        this.getCommentList(this.currentPage, this.pageSize);
        //成功提示
        this.$message.success("删除成功");
      }
    },
    //通过
    async passComment(id) {
      await api.passComment({ id });
      this.getCommentList(this.currentPage, this.pageSize);
    },
    //拒绝
    async refuseComment(id) {
      await api.refuseComment({ id });
      this.getCommentList(this.currentPage, this.pageSize);
    },
    //操作函数 传入path(api对应接口--->string)
    async batchHandlerComment(url) {
      const res = await api[url]({
        batchList: this.multipleSelection, //需要切换状态的数组
      });
      if (res.success) {
        //重新渲染
        this.getCommentList(this.currentPage, this.pageSize);
        this.$message({
          message: "操作成功",
          type: "success",
        });
      } else {
        this.$message.error("错误!!!");
      }
    },
    //打开回复弹框
    replyOpen(data) {
      this.dialogVisible = true;
      this.info = data;
      this.replyValue = "";
    },
    //回复评论
    async replyComment() {
      const res = await api.addCommentList({
        email: this.info.email,
        content: this.replyValue,
        article_id: this.info.article_id,
        parent_content: this.info.content,
        parent_id: this.info.id,
        author: this.info.author,
      });
      //判断是否成功
      if (res.success) {
        //重新渲染
        this.getCommentList(this.currentPage, this.pageSize);
        //成功提示
        this.$message.success("评论成功");
        this.dialogVisible = false;
      } else {
        this.$message.warning("意外的错误");
      }
    },
  },
  created() {
    //初始化数据
    this.getCommentList(this.currentPage, this.pageSize);
  },
};
</script>

<style lang="scss" scoped>
.search-box {
  width: 95%;
  margin: 0 auto;
  margin-top: 25px;
  margin-bottom: 15px;
  .input-box {
    display: flex;
  }
}
.input-box .title-input-box {
  margin: 0 20px;
  display: flex;
  align-items: center;

  div {
    width: 36px;
  }
  .el-input {
    width: 250px;
    margin-left: 10px;
  }
}
.search-btn-box {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  .el-button {
    height: 35px;
    line-height: 12px;
  }
}
.table-box {
  width: 95%;
  margin: 0 auto;
  margin-top: 25px;
  margin-bottom: 15px;
}
.new-create {
  display: flex;
  align-items: center;
  justify-content: space-between;
  i {
    margin-left: 15px;
  }
  .el-button {
    height: 35px;
    line-height: 12px;
  }
}
.el-table {
  overflow: none;
}
.pageination-box {
  display: flex;
  align-content: center;
  justify-content: flex-end;
}
.batch-handler-box {
  .el-button {
    padding: 0px 16px;
    border-radius: 2px;
  }
  .del-btn {
    padding: 6px 16px;
    margin-left: 12px;
    background-color: #fff;
    border: 1px solid #f56c6c;
    color: #f56c6c;
    border-radius: 2px;
    cursor: pointer;
  }
  .del-btn:hover {
    border: 1px solid #f77979;
    color: #f77979;
  }
}
</style>
